package com.fmdj.nebula.service.impl;

import cn.hutool.core.util.IdUtil;
import com.fmdj.common.exception.GlobalException;
import com.fmdj.nebula.db.dao.OrderMonitoringDao;
import com.fmdj.nebula.db.dao.OrderVoiceTextDao;
import com.fmdj.nebula.db.pojo.OrderVoiceTextEntity;
import com.fmdj.nebula.service.MonitoringService;
import com.fmdj.nebula.task.VoiceTextCheckTask;
import io.minio.MinioClient;
import io.minio.PutObjectArgs;
import io.minio.errors.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

@Slf4j
@Service
public class MonitoringServiceImpl implements MonitoringService {

    @Autowired
    private OrderVoiceTextDao orderVoiceTextDao;

    @Value("${minio.endpoint}")
    private String endpoint;

    @Value("${minio.secret-key}")
    private String secretKey;

    @Value("${minio.access-key}")
    private String accessKey;


    @Autowired
    private OrderMonitoringDao orderMonitoringDao;

    @Autowired
    private VoiceTextCheckTask voiceTextCheckTask;

    /**
     *监控上传录音文件
     * @param file 录音文件
     * @param name 保存到Minio中的文件名
     * @param text 录音文件转成的文本内容
     */
    @Override
    @Transactional
    public void monitoring(MultipartFile file, String name, String text) {
        try {
            //连接minio
            MinioClient client = new MinioClient.Builder().endpoint(endpoint).credentials(accessKey, secretKey).build();

            //将录音文件上传到minio
            client.putObject(PutObjectArgs.builder()
                    .bucket("fmdj-record")
                    .object(name)
                    .stream(file.getInputStream(), file.getSize(), -1)
                    .contentType("audio/x-mpeg")
                    .build());
        } catch (Exception e) {
            log.info("上传录音文件时出错",e);
            throw new GlobalException("上传录音文件时出错");
        }

        //将录音文本存放到phoenix数据库中
        OrderVoiceTextEntity entity = new OrderVoiceTextEntity();
        String[] temp = name.substring(0, name.lastIndexOf(".mp3")).split("-");
        Long orderId = Long.parseLong(temp[0]);
        entity.setOrderId(orderId);
        String uuid = IdUtil.simpleUUID();
        entity.setUuid(uuid);
        entity.setRecordFile(name); //上传到minio中的文件名
        entity.setText(text);
        orderVoiceTextDao.insert(entity);


        // 异步的方式进行录音文本审核 (每上传一段审核一段)  执行文稿内容审查
        voiceTextCheckTask.checkText(orderId,text,uuid);
    }

    @Override
    @Transactional
    public int insertOrderMonitoring(long orderId) {
        int rows = orderMonitoringDao.insert(orderId);
        if (rows!=1) {
            throw new GlobalException("插入订单监控记录时出错");
        }
        return rows;
    }
}
